#include "adc_dev.h"
#include "sysmanage.h"
#include "epd_show.h"

/* --------------------------------------------------------------------------*/
/**
 * @Synopsis  获取系统电池电量
 *
 * @Param value 电量 
 *
 * @Returns   
 */
/* ----------------------------------------------------------------------------*/
u8_t get_sys_vbat(void)
{
	u8_t vbat;
	u32_t value;
		
	vbat = 0;

	value = system_adc_read();
	if(value >= 0);
	{
		u32_t Voltage;
		
		Voltage = (value * 1000) / 1024; //1000V  (value * 3000mV)/4096
		printf("get adc value %d Voltage=%d mV\n",value,Voltage);
		if(value < ((VBAT_FULL_ENERGE - VBAT_LOW_ENERGE)/2))//电池不够电量
		{
			u16_t bkp=0;
			
			vbat = 0;
			//bkp = read_rtc_bkp1();
			bkp |= SYS_EVT_VBATLOW;
			set_bkp1_reg(bkp);//标记没电
		}
		else if(value >= VBAT_FULL_ENERGE)//电池满电
		{
			u16_t bkp=0;
			
			vbat = 100;
			//bkp = read_rtc_bkp1();
			bkp |= SYS_EVT_VBATFULL;
			set_bkp1_reg(bkp);//标记充满电

			if((bkp & SYS_EVT_VBATFULL) && (bkp & SYS_EVT_VBATLOW))//充电
			{
				devInfodef *info;

				info = devinfo_get();
				info->charge += 1;
				bkp &= (~SYS_EVT_VBATLOW);
				bkp &= (~SYS_EVT_VBATLOW);
				set_bkp1_reg(bkp);
				devinfo_save();//保存充电次数
			}
		}
		else if(value < VBAT_LOW_ENERGE)//ADC值超出下限
		{
			value = VBAT_FULL_ENERGE;
		}
		else
		{
			vbat = (value-VBAT_LOW_ENERGE)*100/(VBAT_FULL_ENERGE-VBAT_LOW_ENERGE);
			printf("vbat=%d\n",vbat);
		}
	}

	return vbat;
}







